package com.example;

import java.io.*;
import java.util.Stack;

public class GameSaveManager {
    // 保存棋盘状态到文件
    public static void saveGame(ChessBoard board, String filePath) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
            oos.writeObject(board.getGrid());
            oos.writeObject(board.getHistory());
            oos.writeInt(board.getCurrentPlayer());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 从文件加载棋盘状态
    public static void loadGame(ChessBoard board, String filePath) {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
            int[][] grid = (int[][]) ois.readObject();
            @SuppressWarnings("unchecked")
            Stack<ChessBoard.Move> history = (Stack<ChessBoard.Move>) ois.readObject();
            int player = ois.readInt();

            // 更新棋盘状态
            board.setGrid(grid);
            board.setHistory(history);
            board.setCurrentPlayer(player);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}